package com.cloudsigns.factorymanager2.media_system.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.adapter.ScrollablePanelAdapter;
import com.cloudsigns.factorymanager2.produce.bean.DateInfo;
import com.cloudsigns.factorymanager2.produce.bean.OrderInfo;
import com.cloudsigns.factorymanager2.produce.bean.RoomInfo;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.kelin.scrollablepanel.library.ScrollablePanel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class MediaSystemPublicationStatusFormActivity extends BaseActivity{
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View ll_op_one, ll_op_two, ll_op_three, ll_op_four;
    private ImageView img_op_one, img_op_two, img_op_three, img_op_four;
    private ImageView img_tri_one, img_tri_two, img_tri_three, img_tri_four;

    private DropDownPoupWindow popupWindow;
    /*标题栏三个按钮类型*/
    public static final int MODULE1 = 0;
    public static final int MODULE2 = 1;
    public static final int MODULE3 = 3;

    public static final SimpleDateFormat DAY_UI_MONTH_DAY_FORMAT = new SimpleDateFormat("MM-dd");
    public static final SimpleDateFormat WEEK_FORMAT = new SimpleDateFormat("EEE", /*Locale.US*/Locale.CHINA);

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_media_system_publication_status_form);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        ll_op_one = findViewById(R.id.ll_op_one);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_three = findViewById(R.id.ll_op_three);
        ll_op_four = findViewById(R.id.ll_op_four);
        img_op_one = findViewById(R.id.img_op_one);
        img_op_two = findViewById(R.id.img_op_two);
        img_op_three = findViewById(R.id.img_op_three);
        img_op_four = findViewById(R.id.img_op_four);
        img_tri_one = findViewById(R.id.img_tri_one);
        img_tri_two = findViewById(R.id.img_tri_two);
        img_tri_three = findViewById(R.id.img_tri_three);
        img_tri_four = findViewById(R.id.img_tri_four);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.publication));

        img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
        img_op_two.setBackgroundResource(R.mipmap.icon_produced);
        img_op_three.setBackgroundResource(R.mipmap.icon_hand);
        img_op_four.setBackgroundResource(R.mipmap.icon_patrol);

        ll_op_one.setVisibility(View.GONE);
        ll_op_two.setVisibility(View.VISIBLE);
        ll_op_three.setVisibility(View.VISIBLE);
        ll_op_four.setVisibility(View.VISIBLE);
        rl_back.setVisibility(View.VISIBLE);

        final ScrollablePanel scrollablePanel = (ScrollablePanel) findViewById(R.id.scrollable_panel);
        final ScrollablePanelAdapter scrollablePanelAdapter = new ScrollablePanelAdapter();
        generateTestData(scrollablePanelAdapter);
        scrollablePanel.setPanelAdapter(scrollablePanelAdapter);
    }

    @Override
    public void setListener() {
        ll_op_one.setOnClickListener(this);
        ll_op_two.setOnClickListener(this);
        ll_op_three.setOnClickListener(this);
        ll_op_four.setOnClickListener(this);
        rl_back.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.ll_op_two:
                  showPublicationPw(view);
                break;
            case R.id.ll_op_four:
                // showSelectPopupWindwo(view);
                break;
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    /**
     * 功能1
     *
     * @param anchor
     */
    private void showPublicationPw(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();

        popupWindow = new DropDownPoupWindow(this, TAG, MODULE2, R.layout.popupwindow_media_system_publication_status, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // backgroundAlpha(0.6f);
        img_tri_two.setVisibility(View.VISIBLE);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                img_tri_two.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }


    private void generateTestData(ScrollablePanelAdapter scrollablePanelAdapter) {
        List<RoomInfo> roomInfoList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            RoomInfo roomInfo = new RoomInfo();
            roomInfo.setRoomType("");
            roomInfo.setRoomId(i);
            roomInfo.setRoomName("20" + i);
            roomInfoList.add(roomInfo);
        }
        for (int i = 6; i < 30; i++) {
            RoomInfo roomInfo = new RoomInfo();
            roomInfo.setRoomType("");
            roomInfo.setRoomId(i);
            roomInfo.setRoomName("30" + i);
            roomInfoList.add(roomInfo);
        }
        scrollablePanelAdapter.setRoomInfoList(roomInfoList);

        List<DateInfo> dateInfoList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 14; i++) {
            DateInfo dateInfo = new DateInfo();
            String date = DAY_UI_MONTH_DAY_FORMAT.format(calendar.getTime());
            String week = WEEK_FORMAT.format(calendar.getTime());
            dateInfo.setDate(date);
            dateInfo.setWeek(week);
            dateInfoList.add(dateInfo);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        scrollablePanelAdapter.setDateInfoList(dateInfoList);

        List<List<OrderInfo>> ordersList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            List<OrderInfo> orderInfoList = new ArrayList<>();
            for (int j = 0; j < 14; j++) {
                OrderInfo orderInfo = new OrderInfo();
//                orderInfo.setGuestName("NO." + i + j);
//                orderInfo.setGuestName("");
                orderInfo.setBegin(true);
                orderInfo.setStatus(OrderInfo.Status.randomStatus());
                if (orderInfoList.size() > 0) {
                    OrderInfo lastOrderInfo = orderInfoList.get(orderInfoList.size() - 1);
                    if (orderInfo.getStatus().ordinal() == lastOrderInfo.getStatus().ordinal()) {
                        orderInfo.setId(lastOrderInfo.getId());
                        orderInfo.setBegin(false);
                        orderInfo.setGuestName("");
                    } else {
                        if (new Random().nextBoolean()) {
                            orderInfo.setStatus(OrderInfo.Status.BLANK);
                        }
                    }
                }
                orderInfoList.add(orderInfo);
            }
            ordersList.add(orderInfoList);
        }
        scrollablePanelAdapter.setOrdersList(ordersList);
    }
}
